﻿using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System;
using System.Windows.Media.Imaging;

namespace BssDs.Client.UI.Controls.ExtendedControl.Tab
{
	/// <summary>
	/// NewAddTabItem.xaml の相互作用ロジック
	/// </summary>
	public partial class NewAddTabItem : TabItem
	{
		public NewAddTabItem()
		{
			InitializeComponent();
		}

		/// <summary>
		/// ルーティングイベントの登録
		/// </summary>
		public static readonly RoutedEvent NewTabItemAddButtonClickEvent =
			EventManager.RegisterRoutedEvent("NewTabItemAddButtonClick", RoutingStrategy.Bubble,
			typeof(RoutedEventHandler), typeof(NewAddTabItem));

		/// <summary>
		/// 新規追加タブのクリックイベント
		/// </summary>
		public event RoutedEventHandler NewTabItemAddButtonClick
		{
			add
			{
				AddHandler(NewTabItemAddButtonClickEvent, value);
			}
			remove
			{
				RemoveHandler(NewTabItemAddButtonClickEvent, value);
			}
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			this.SetNewAddButtonImage(false);

			NewTabItemAddButton.Click += new RoutedEventHandler(NewTabItemAddButton_Click);
			NewTabItemAddButton.MouseEnter += new MouseEventHandler(NewTabItemAddButton_MouseEnter);
			NewTabItemAddButton.MouseLeave += new MouseEventHandler(NewTabItemAddButton_MouseLeave);
		}

		protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
		{
			e.Handled = false;
		}


		/// <summary>
		/// 新規タブ追加用のTabItemを押下したときの処理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void NewTabItemAddButton_Click(object sender, RoutedEventArgs e)
		{
			// NewTabItemAddButtonClickイベントを発生させる
			this.RaiseEvent(new RoutedEventArgs(NewTabItemAddButtonClickEvent));
		}

		/// <summary>
		/// 新規タブ追加用のTabItemにマウスカーソルを移動したときの処理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void NewTabItemAddButton_MouseEnter(object sender, MouseEventArgs e)
		{
			this.SetNewAddButtonImage(true);
		}

		/// <summary>
		/// 新規タブ追加用のTabItemからマウスカーソルを移動したときの処理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void NewTabItemAddButton_MouseLeave(object sender, MouseEventArgs e)
		{
			this.SetNewAddButtonImage(false);
		}


		public void SetNewAddButtonImage(bool isSelected)
		{
			Uri uri;

			if (isSelected)
			{
				uri = new Uri("/BuiltInResource;component/Images/112_Plus_Green_16x16_72.png", UriKind.Relative);
			}
			else
			{
				uri = new Uri("/BuiltInResource;component/Images/112_Plus_Grey_16x16_72.png", UriKind.Relative);
			}

			this.NewTabItemAddImage.Source = new BitmapImage(uri);
		}
	}
}
